package com.pig4cloud.pig.module.controller.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.module.api.entity.electric.ElectricConcentrator;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.service.electric.IElectricConcentratorService;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 电485集中器Controller
 *
 * @author ruoyi
 * @date 2022-09-01
 */
@Validated
@Api(value = "电485集中器控制器", tags = {"电485集中器管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/device/concentrator")
public class ElectricConcentratorController {

    private final IElectricConcentratorService electricConcentratorService;
    private final IPropertyService propertyService;
    private final IEstateService estateService;
    /**
     * 查询电485集中器列表
     */
    @ApiOperation("查询电485集中器列表")
    @PreAuthorize("@pms.hasPermission('module:concentrator:list')")
    @GetMapping("/list")
    public TableDataInfo<ElectricConcentrator> list(@Validated ElectricConcentrator electricConcentrator) {
        QueryWrapper<ElectricConcentrator> qw = new QueryWrapper<>();
        PigUser sysUser = SecurityUtils.getUser();
		if(sysUser.getUserType() == 6 || sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
		}else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(electricConcentrator.getEstateId()!=null){
				qw.eq("estate_id",electricConcentrator.getEstateId());
			}
		}
        TableDataInfo<ElectricConcentrator> td = PageUtils.buildDataInfo(electricConcentratorService.page(PageUtils.buildPage(),qw));
        List<ElectricConcentrator> list = td.getData();
        return td;
    }

    /**
     * 导出电485集中器列表
     */
    @ApiOperation("导出电485集中器列表")
    @PreAuthorize("@pms.hasPermission('module:concentrator:export')")
    @SysLog("电485集中器")
    @GetMapping("/export")
    public List export(@Validated ElectricConcentrator electricConcentrator) {
        List<ElectricConcentrator> list = electricConcentratorService.list();
        // ExcelUtil<ElectricConcentrator> util = new // ExcelUtil<ElectricConcentrator>(ElectricConcentrator.class);
        return  list;// util.exportExcel(list, "电485集中器");
    }

    /**
     * 获取电485集中器详细信息
     */
    @ApiOperation("获取电485集中器详细信息")
    @PreAuthorize("@pms.hasPermission('module:concentrator:query')")
    @GetMapping("/{id}")
    public R<ElectricConcentrator> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ElectricConcentrator electricConcentrator = electricConcentratorService.getById(id);
        return R.ok(electricConcentrator);
    }

    /**
     * 新增电485集中器
     */
    @ApiOperation("新增电485集中器")
    @PreAuthorize("@pms.hasPermission('module:concentrator:add')")
    @SysLog("电485集中器")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ElectricConcentrator electricConcentrator) {
        if(electricConcentrator.getPropertyId()!=null){
            Property property=propertyService.findById(electricConcentrator.getPropertyId());
            electricConcentrator.setPropertyName(property.getPropertyName());
        }
        if(electricConcentrator.getEstateName()!=null){
            Estate estate=estateService.findById(electricConcentrator.getEstateName());
            electricConcentrator.setEstateName(estate.getEstateName());
        }
        return R.okByRow(electricConcentratorService.save(electricConcentrator) ? 1 : 0);
    }

    /**
     * 修改电485集中器
     */
    @ApiOperation("修改电485集中器")
    @PreAuthorize("@pms.hasPermission('module:concentrator:edit')")
    @SysLog("电485集中器")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ElectricConcentrator electricConcentrator) {
        if(electricConcentrator.getPropertyId()!=null){
            Property property=propertyService.findById(electricConcentrator.getPropertyId());
            if (property!=null){
                electricConcentrator.setPropertyName(property.getPropertyName());
            }
        }
        if(electricConcentrator.getEstateName()!=null){
            Estate estate=estateService.findById(electricConcentrator.getEstateId());
            if (estate!=null){
                electricConcentrator.setEstateName(estate.getEstateName());
            }

        }
        return R.okByRow(electricConcentratorService.updateById(electricConcentrator) ? 1 : 0);
    }

    /**
     * 删除电485集中器
     */
    @ApiOperation("删除电485集中器")
    @PreAuthorize("@pms.hasPermission('module:concentrator:remove')")
    @SysLog("电485集中器")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(electricConcentratorService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
